package cn.fangline.wo.service;


import cn.fangline.base.enums.CommentType;
import cn.fangline.base.util.exception.BussinessException;
import cn.fangline.wo.model.House;
import cn.fangline.wo.model.Message;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * Created by 抽离 on 2017/5/16 0016.
 */
@Service
public class MessageService {


    public void addComment(Message message)throws BussinessException{
        if(message == null){
            throw new BussinessException("参数错误");
        }

        House house = new House();
        house = house.selectById(message.getHouseId());
        Message messageQuery = new Message().selectOne(new EntityWrapper<Message>()
                .eq("house_id", message.getHouseId())
                .eq("user_name", message.getUserName())
                .eq("avatar_url", message.getAvatarUrl())
        );
        if(messageQuery != null){
            throw  new  BussinessException("你已经评价过该房源了");
        }
        else {
            message.setSendTime(new Date());
            CommentType ct = CommentType.getCommentString(message.getMsgType());

            if(ct != null) {
                message.setMsgContent(ct.getCommentString() + house.getSection() + house.getRoom() + "室" + house.getHall() + "厅" + house.getTotalPrice() +"万元");
            }
            message.insert();
        }
    }
}
